package com.huarui.saleReturn.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.part.model.PartPropertySearch;
import com.huarui.sale.model.Sale;
import com.huarui.saleReturn.service.SaleReturnService;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.util.JsonUtil;


public class SaleReturnQueryAction extends BaseAction {

	private static final long serialVersionUID = 969945153201681221L;
	private Map<String, Object> jsonObject;
	private SaleReturnService saleReturnService;
	private String queryParam;
	@SuppressWarnings("unchecked")
	private ListModel saleListModel;
	
	// 分页
	private Integer start;
	private Integer limit;
	private String sort;
	private String dir;	
	
	private String searchJson;
	/**
	 * 单据号
	 */
	private String codeNumber;

	
	/**
	 * 查询销售单和急件单明细
	 * @author jiwei.yi
	 * @return
	 */
	public String queryAllItems() {		
		jsonObject = new HashMap<String, Object>();		
		Sale sale = saleReturnService.getSaleByConditions(queryParam);				
		if (sale == null) {
			jsonObject.put("success", false);
			jsonObject.put("msg", ",未查询到相关销售出库单,请输入正确的查询数据!");
		} else {
			Map<String, Object> queryMap = new HashMap<String, Object>();
			//查询是否有库存件
			queryMap.put("saleId", sale.getId());
			queryMap.put("varGroup", DictionaryEnum.GROUP_SOURCE.getGroup());
			List<Map<String, Object>> saleItems = saleReturnService.getSaleItem(queryMap);
			if (saleItems.size() > 0) {
				jsonObject.put("saleItem", saleItems);
			}			
			// 查询是否有急件
			List<Integer> ids = saleReturnService.getExpressIdsBySaleId(sale.getId());
			if(!ids.isEmpty()){
				List<Map<String, Object>> expressItems = saleReturnService.getExpressItemForMapByExpressIds(ids);
				if(expressItems.size() > 0 ){
					jsonObject.put("expressItem", expressItems);
				}
			}						
			jsonObject.put("success", true);
		}
			return JSON;
	}
	
	
	@SuppressWarnings("unchecked")
	public String fillSaleReturnBrowser(){
		jsonObject = new HashMap<String,Object>();
		if (queryParam != null) {
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);
		}
		saleListModel = ListModelParamUtil();
		ListModel saleReturns = saleReturnService.getSaleReturnListModelByConditions(jsonObject,saleListModel);
		jsonObject.clear();
		jsonObject.put(ConstantParamEnum.TOTALPROPERTY.getType(), saleReturns.getTotal());
		jsonObject.put(ConstantParamEnum.ROOT.getType(), saleReturns.getResult());
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String fillSaleReturnItemBrowser(){
		jsonObject = new HashMap<String,Object>();
		if (queryParam != null) {
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);
		}
		if(searchJson!=null){
			PartPropertySearch partPropertySearch=JsonUtil.getBean(searchJson, PartPropertySearch.class);
			jsonObject.put("searchJson", partPropertySearch);
		}
		saleListModel = ListModelParamUtil();
		ListModel saleReturnItemListModel = saleReturnService.getSaleReturnItemListModelByConditions(jsonObject,saleListModel);
		jsonObject.clear();
		jsonObject.put(ConstantParamEnum.TOTALPROPERTY.getType(), saleReturnItemListModel.getTotal());
		jsonObject.put(ConstantParamEnum.ROOT.getType(), saleReturnItemListModel.getResult());
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	private ListModel ListModelParamUtil() {
		saleListModel = new ListModel();
		saleListModel.setPage(start ==null && limit == null ? 1: start / limit + 1);
		saleListModel.setPageSize(limit == null ? Integer.valueOf(ConstantParamEnum.PAGESIZE.getType()) : limit);
		saleListModel.setOrderBy(sort);
		if (dir != null){
			saleListModel.setDesc(dir.equals(ConstantParamEnum.DESC.getType()) ? true : false);
		}else{
			saleListModel.setDesc(true);
		}	
		return saleListModel;
	}

	

	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(Map<String, Object> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getCodeNumber() {
		return codeNumber;
	}

	public void setCodeNumber(String codeNumber) {
		this.codeNumber = codeNumber;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setSaleReturnService(SaleReturnService saleReturnService) {
		this.saleReturnService = saleReturnService;
	}

	@SuppressWarnings("unchecked")
	public ListModel getSaleListModel() {
		return saleListModel;
	}

	@SuppressWarnings("unchecked")
	public void setSaleListModel(ListModel saleListModel) {
		this.saleListModel = saleListModel;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}


	public void setSearchJson(String searchJson) {
		this.searchJson = searchJson;
	}

}
